home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* getmidi.c link with -lmidi -lm -lC */
- /* usage: getmidi: <-cchannel> */
-
- #include <midi.h>
- #include <miditime.h>
- #include <midiio.h>
- #include <signal.h>
- /* for signal handler */
-
- #define ALLCHANNELS 0xffff
-
- int caught_sigint = 0; /* completion flag */
- MItime timeofday; /* time of day of first event */
- long gmlisten = ALLCHANNELS;
-
- void
- catch_sigint()
- {
- caught_sigint=1;
- }
-
- void printconfig(MIconfig *miconfig)
- {
- MItime mitime;
-
- printf("MICstamp\n Timestamping is ");
- switch(MICstamp(miconfig))
- {
- case MINOSTAMP:
- {
- printf("MINOSTAMP:no timestamp\n");
- break;
- }
- case MIDELTASTAMP:
- {
- printf("MIDELTASTAMP:timestamp since previous event\n");
- break;
- }
- case MIRELSTAMP:
- {
- printf("MIRELSTAMP:timestamp since first event\n");
- break;
- }
- default:
- {
- printf("unknown timestamping type\n");
- }
- }
-
- printf("MICblocking\n Blocking is ");
- switch(MICblocking(miconfig))
- {
- case MIBLOCKING:
- {
- printf("MIBLOCKING:wait until message received\n");
- break;
- }
- case MINONBLOCKING:
- {
- printf("MINONBLOCKING:no waiting until message received\n");
- printf(" MICtimeout - will wait for ");
- /*
- mitime = MICtimeout(miconfig);
- printf("%f seconds.\n",(float)MItimegettotalsecs(mitime)+((float)MItimegetmicros(mitime) / 1000000.0));
- */
- break;
- }
- default:
- {
- printf("unknown blocking type\n");
- }
- }
-
- /* Need another example of MIdevice, thisone's outdated
- printf("MICdevice\n Device is %d.\n", MICdevice(miconfig));
- */
- }
-
-
- void printMImessage(MImessage mimsg)
- /* Keep in mind this is done with what I could make out from the
- MIDI specification from the klunker Roland Juno-106 */
- {
- printf(" device = %d, channel = %d, length = %d.\n",
- MIdevice(mimsg), MIchannel(mimsg), MIlength(mimsg));
- printf(" status = ");
- switch(MIstatus(mimsg) & MIDI_StatusMask)
- {
- /* case MIDI_ChannelVoice: <-- these are the same */
- case MIDI_NoteOff: /* 0x80 - 1000 0000 */
- {
- printf("Note %x Off - velocity %x\n",
- MIbyte1(mimsg), MIbyte2(mimsg));
- break;
- }
- case MIDI_NoteOn: /* 0x90 - 1001 0000 */
- {
- if(MIbyte2(mimsg) == 0) /* this is equivalent */
- /* to a MIDI_NoteOff */
- {
- printf("Note %x Off - velocity 00\n",
- MIbyte1(mimsg));
- }
- else
- {
- printf("Note %x On - velocity %x\n",
- MIbyte1(mimsg), MIbyte2(mimsg));
- }
- break;
- }
- case MIDI_PolyKeyPressure: /* 0xa0 - 1010 0000 */
- {
- printf("PolyKeyPressure - %x\n",MIbyte2(mimsg));
- break;
- }
- /* case MIDI_ControlChange: <-- these are the same */
- case MIDI_ChannelModeSelect: /* 0xb0 - 1011 0000 */
- {
- switch(MIbyte1(mimsg))
- {
- case MC_DamperPedal: /* 0x40 - 0100 0000 */
- {
- printf("Damper Pedal at %x\n",
- MIbyte2(mimsg));
- }
- case MC_AllNotesOff: /* 0x7b - 0111 1011 */
- {
- printf("All Notes Off at %x\n",
- MIbyte2(mimsg));
- }
- case MC_OmniModeOff: /* 0x7c - 0111 1100 */
- {
- printf("Omni Mode Off. next byte:%x\n",
- MIbyte2(mimsg));
- }
- case MC_OmniModeOn: /* 0x7d - 0111 1101 */
- {
- printf("Omni Mode On. next byte:%x\n",
- MIbyte2(mimsg));
- }
- case MC_MonoModeOn: /* 0x7e - 0111 1110 */
- {
- printf("Mono Mode On. next byte:%x\n",
- MIbyte2(mimsg));
- }
- case MC_PolyModeOn: /* 0x7f - 0111 1111 */
- {
- printf("Poly Mode On. next byte:%x\n",
- MIbyte2(mimsg));
- }
- case MC_ModulationWheel:/* 0x01 - 0000 0001 */
- {
- printf("Modulation Wheel at %x\n",
- MIbyte2(mimsg));
- }
- }
- break;
- }
- case MIDI_ProgramChange: /* 0xc0 - 1100 0000 */
- {
- printf("Program Change to #%x.\n",
- MIbyte1(mimsg));
- break;
- }
- case MIDI_ChannelPressure: /* 0xd0 - 1101 0000 */
- {
- printf("Channel Pressue - %x.\n",MIbyte1(mimsg));
- break;
- }
- case MIDI_PitchBendChange: /* 0xe0 - 1110 0000 */
- {
- printf("Pitch Bender at %x.\n",
- 128 * MIbyte2(mimsg) + MIbyte1(mimsg));
- break;
- }
- }
- }
-
-
- void printevents(MIevent *mievent, MItype *mitype, int num_events)
- {
- /*
- Remember: within the MIevent structure is the timestamp and the message
- buffer. within the message buffer lies the MImessage structure and the buffer for system exclusive dats.
- */
-
- MItime mitime;
- MImessage mimsg;
- unsigned char *misysexbuf;
- int i,j;
- static long eventnum = 0;
-
- if(!eventnum) /* first event get time of day */
- {
- timeofday = MItimecurrentsystemtime(&timeofday);
- printf(" STARTING time %d:%d:%f.\n",
- MItimegethours(&timeofday),
- MItimegetminutes(&timeofday),
- (float)MItimegetsecs(&timeofday) +
- ((float)MItimegetmicros(&timeofday) / 1000000.0));
- eventnum++;
- }
-
- for (i=0; i < num_events; i++)
- {
- mievent[i].dt = MItimecurrentsystemtime(&mievent[i].dt);
- mitime = MItimesubtract(&mievent[i].dt,&timeofday);
- mimsg = mievent[i].mm.msgbuf;
- mitype = &mievent[i].t;
- misysexbuf = mievent[i].mm.sysexbuf;
- if( (*mitype != MIMESSAGE) ||
- ( (*mitype == MIMESSAGE) &&
- ( (gmlisten == ALLCHANNELS) ||
- ( (gmlisten!=ALLCHANNELS)&&(MIchannel(mimsg)==gmlisten)))))
- {
- printf("EVENT #%d - ",eventnum++);
- switch(*mitype)
- {
- case MIMESSAGE:
- {
- printf("MIMESSAGE\n");
- printMImessage(mimsg);
- break;
- }
- case MISYSEX:
- {
- printf("MISYSEX\n");
-
- printf("%d bytes: ",mievent[i].count);
- for(j=0; (j<mievent[i].count-1)&&(j<8); j++)
- printf("%x, ",misysexbuf[j]);
- printf("%x %s\n",misysexbuf[j],
- ( (mievent[i].count > 8) ? "..." : ""));
- break;
- }
- case MIREALTIME:
- {
- printf("MIREALTIME\n");
- break;
- }
- case MISYSCOM:
- {
- printf("MISYSCOM\n");
- break;
- }
- default:
- {
- printf("unknown\n");
- break;
- }
-
- }
-
- printf(" at time %lf\n", MItimedouble(&mitime)/1000000.0 );
- }
- }
- }
-
-
- main(int argc, char **argv)
- {
- MIconfig *miconfig = MInewconfig();
- /* returned a null pointer */
- MItime *mitime;
- const MItime *mitimeStart;
- MIevent mievent[16];
- MItype mitype[16];
- MIport *miPort = MInewport();
- /* returned a null pointer */
- long hrs,mins,secs;
- int num;
-
- /* config stuff */
-
- if(argc>1)
- {
- if(strncmp(argv[1],"-c",2) == 0)
- {
- gmlisten = atoi(argv[1]+2);
- }
- }
- if(gmlisten != ALLCHANNELS)
- printf("listening to MIDI channel %d.\n",gmlisten);
- else
- printf("listening to all MIDI channels.\n");
-
-
- printf("DEFAULT CONFIGURATION:\n");
- printconfig(miconfig); /* print out default configuiration */
-
- MICsetstamp(miconfig,MIRELSTAMP);
- MICsetblocking(miconfig,MINONBLOCKING);
- /* necessary for this program */
-
- printf("NEW CONFIGURATION:\n");
- printconfig(miconfig); /* print out the new config */
-
- sigset(SIGINT, catch_sigint);
- /* set signal handler */
- /* to catch ctrl-c */
-
- if (MIopen(miPort,"r", miconfig) == -1) /* open midi port */
- {
- printf("MIopen: Couldn't open midi port\n");
- exit(1);
- }
- printf("MIopen succeded.\n");
-
- printf("MIgetfd: file descriptor is %x\n.", MIgetfd(miPort));
-
- while(!caught_sigint)
- {
- num = MIreceive(miPort,mievent,1);
- /* block here for 16 midi events */
- if(num == -1)
- {
- printf("Error in MIrecieve.\n");
- exit(1);
- }
-
- printevents(mievent,mitype,num); /* show what we got */
- }
-
- MIclose(miPort); /* close midi port */
-
- /* free memory */
- free(miPort);
- free(miconfig);
- }
-